package com.ztx.service.user.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import com.ztx.model.ResourceVO;
import com.ztx.model.RoleVO;
import com.ztx.model.User;
import com.ztx.model.UserRole;
import com.ztx.pagemodel.easyui.DataGrid;
import com.ztx.pagemodel.easyui.PageHelper;
import com.ztx.pagemodel.easyui.Tree;
import com.ztx.service.user.IUserService;
import com.ztx.utils.Utils;

@Repository(IUserService.BEAN_ID)
public class UserServiceImpl implements IUserService {

	private SqlSessionTemplate sqlSessionTemplate;

	@Resource
	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
	}

	@Override
	public User login(String username, String password) {
		String md5Password = DigestUtils.md5Hex(password);
		Map<String, String> param = new HashMap<String, String>();
		param.put("username", username);
		param.put("password", md5Password);
		User u = sqlSessionTemplate.selectOne(
				User.class.getName() + ".getUser", param);
		return u;
	}

	@Override
	public List<String> resourceList(String id) {
		List<String> resourceList = new ArrayList<String>();
		List<RoleVO> roles = sqlSessionTemplate.selectList(
				RoleVO.class.getName() + ".getRoleAndResource", id);
		if (roles != null && roles.size() != 0) {
			for (RoleVO role : roles) {
				Set<ResourceVO> resources = role.getResources();
				if (resources != null && !resources.isEmpty()) {
					for (ResourceVO resource : resources) {
						resourceList.add(resource.getUrl());
					}
				}

			}
		}
		return resourceList;
	}

	@Override
	public DataGrid<User> userDataGrid(User user, PageHelper ph) {
		DataGrid<User> datagrid = new DataGrid<User>();
		long total = sqlSessionTemplate.selectOne(User.class.getName()
				+ ".getallUserCount", user);
		List<User> userList = sqlSessionTemplate.selectList(
				User.class.getName() + ".getAllUser", user, ph.getRowBounds());
		datagrid.setTotal(total);
		datagrid.setRows(userList);
		return datagrid;
	}

	@Override
	public DataGrid<RoleVO> roleDataGrid(PageHelper ph, RoleVO role) {
		DataGrid<RoleVO> datagrid = new DataGrid<>();
		long total = sqlSessionTemplate.selectOne(RoleVO.class.getName()
				+ ".getAllRolesCount", role);
		List<RoleVO> roles = sqlSessionTemplate.selectList(
				RoleVO.class.getName() + ".getAllRoles", role,
				ph.getRowBounds());

		datagrid.setTotal(total);
		datagrid.setRows(roles);
		return datagrid;
	}

	@Override
	public void addUser(User user) throws Exception {
		user.setId(Long.toString(Utils.createId()));
		user.setPassword(DigestUtils.md5Hex(user.getId()));
		user.setStatus("0");
		sqlSessionTemplate.insert(User.class.getName() + ".addUser", user);
	}

	@Override
	public List<Tree> getUserRoles(String userId) throws Exception {
		// 查询所有的角色
		List<RoleVO> allRoles = sqlSessionTemplate.selectList(
				RoleVO.class.getName() + ".getAllRoles", null);
		// 查询用户拥有的角色
		List<RoleVO> userRoles = sqlSessionTemplate.selectList(
				RoleVO.class.getName() + ".getUserRoleByUserId", userId);
		Set<String> checkedRoleIds = new HashSet<>();
		for (RoleVO cr : userRoles) {
			checkedRoleIds.add(cr.getId());
		}

		List<Tree> listTree = new ArrayList<>();
		for (RoleVO r : allRoles) {
			Tree tree = new Tree();
			tree.setText(r.getName());
			tree.setId(r.getId());
			if (checkedRoleIds.contains(r.getId())) {
				tree.setChecked(true);
			}
			listTree.add(tree);
		}
		return listTree;
	}

	@Override
	public void saveUserRole(String userId, String[] roleIds) throws Exception {
		sqlSessionTemplate.delete(
				User.class.getName() + ".delUserRoleByUserid", userId);
		UserRole ur = new UserRole();
		ur.setUserId(userId);
		for (String roleId : roleIds) {
			ur.setId(Utils.createId());
			ur.setRoleId(roleId);
			sqlSessionTemplate
					.insert(User.class.getName() + ".addUserRole", ur);
		}
	}
}
